package JDBC;

import com.mysql.jdbc.Statement;
import utils.JDBCUtils;

import java.sql.*;
/*
     演示工具类的作用
*/

public class jdbcdemo04 {
    public static void main(String[] args) {

        Statement stmt=null;
        Connection conn =null;
        ResultSet rs =null;

            try {
                /*// 1 注册驱动
                Class.forName("com.mysql.jdbc.Driver");
                // 2 获取连接对象
                bb = DriverManager.getConnection("jdbc:mysql://localhost:3306/db3", "root", "root118");*/
                conn = JDBCUtils.ConnectionUtils();
                // 3 :定义sql
                String aa = "select * from account";
                // 4 获取执行sql的对象
                stmt = (com.mysql.jdbc.Statement) conn.createStatement();
                // 5 执行sql
                rs = stmt.executeQuery(aa);
                // 6 处理结果
                //  循环判断游标是否到达了最后一行  while和for循环都可以
                while(rs.next()){
                    //获取数据
                    int mm = rs.getInt(1);
                    String nn = rs.getString("name");
                    double xx = rs.getDouble(3);
                    System.out.println(mm + "------" + nn +"------" + xx );
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();

        }finally {
            // 7释放资源
            // 为了防止空指针异常
           /* if (rs!= null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (cc!= null){
                try {
                    cc.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (bb!= null){
                try {
                    bb.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }*/

            JDBCUtils.Close(rs,stmt,conn);

        }
    }
}
